package com.zpark.merchant.app.controller;

import com.zpark.jushanpay.common.domain.BusinessException;
import com.zpark.jushanpay.common.domain.RestResponse;
import com.zpark.jushanpay.common.util.SecurityUtil;
import com.zpark.merchant.app.feign.ITransactionPayChannelFeign;
import com.zpark.transaction.api.dto.PayChannelDTO;
import com.zpark.transaction.api.dto.PayChannelParamDTO;
import com.zpark.transaction.api.dto.PlatformChannelDTO;
import feign.Feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 支付渠道相关
 */
@RestController
@RequestMapping("/pf_param")
public class PlatformParamController {

    @Autowired
    private ITransactionPayChannelFeign payChannelFeign;

    /**
     * 查询所有平台服务类型数据
     * @return
     */
    @GetMapping("/find_pf_channel_all")
    public RestResponse<List<PlatformChannelDTO>> findPlatformChannelAll(){
        // 使用feign远程调用服务接口
        RestResponse<List<PlatformChannelDTO>> all = payChannelFeign.findPlatformChannelAll();
        // 返回结果
        return all;
    }

    /**
     * 应用绑定服务
     * @param appId 应用的appId
     * @param platformChannelCode 平台服务类型
     * @return
     */
    @PostMapping("/bind_app_platform/{appId}")
    public RestResponse<Void> bindPlatformChannelForApp(
            @PathVariable String appId, String platformChannelCode){
        // 调用feign接口实现应用绑定
        return payChannelFeign.bindPlatformChannelForApp(appId, platformChannelCode);
    }

    /**
     * 查询应用与平台支付渠道类型是否绑定
     * @param appId 应用的appId
     * @param platformChannelCode 应用平台服务类型编码
     * @return
     */
    @PostMapping("/find_is_bind_app_platform_channel/{appId}")
    public RestResponse<Integer> findIsBindAppPlatformChannel(@PathVariable String appId,
                                                          String platformChannelCode){
        // 调用feign接口查询数据
        return payChannelFeign.findAppPlatformChannelByAppIdChannelCode(appId, platformChannelCode);
    }

    /**
     * 根据平台服务类型查询原始支付渠道数据
     * @param platformChannelCode 平台服务类型编码
     * @return
     */
    @PostMapping("/find_app_channel_by_platform_channel_code")
    public RestResponse<List<PayChannelDTO>> findAppChannelByPlatformChannelCode(String platformChannelCode){
        // 调用feign接口查询数据
        // 返回结果
        return payChannelFeign.findPayChannelByPlatformChannel(platformChannelCode);
    }

    /**
     * 保存支付渠道参数配置
     * @param dto 支付渠道参数对象
     * @return
     */
    @PostMapping("/save_add_pay_channel_param")
    public RestResponse<Void> saveAddPayChannelParam(@RequestBody PayChannelParamDTO dto){
        // 从token当中解析merchantId
        Long merchantId = SecurityUtil.getMerchantId();
        dto.setMerchantId(merchantId);
        // 调用feign接口实现支付参数保存
        return payChannelFeign.addPayChannelParam(dto);
    }

    /**
     * 根据应用的appId和平台服务类型编码查询原始支付渠道参数配置数据
     * @param appId 应用的appId
     * @param platformChannelCode 平台服务类型编码
     * @return
     */
    @PostMapping("/find_pcp_by_app_and_platform/{appId}")
    public RestResponse<List<PayChannelParamDTO>> findPayChannelParamByAppAndPlatform(
            @PathVariable("appId") String appId, String platformChannelCode) throws BusinessException {
        // 调用Feign接口方法查询数据
        return payChannelFeign.findPayChannelParamByAppAndPlatform(appId, platformChannelCode);
    }

    /**
     * 根据应用的appId和平台服务类型编码以及原始支付渠道编码查询原始支付渠道参数配置数据
     * @param appId 应用的appId
     * @param platformChannelCode 平台服务类型编码
     * @param payChannel 原始支付渠道编码
     * @return
     */
    @PostMapping("/find_pcp_by_app_and_ppc/{appId}")
    public RestResponse<PayChannelParamDTO> findPayChannelParamByAppAndPlatform(
            @PathVariable("appId") String appId, String platformChannelCode,
             String payChannel) throws BusinessException {
        // 调用Feign接口方法查询数据
        return payChannelFeign.findPayChannelParamByAppAndPlatform(appId, platformChannelCode, payChannel);
    }
}
